<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="../include/tag.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>选择礼包</title>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/common/table.css">
    <%@include file="../include/commonFile.jsp" %>
    <style type="text/css">
        .index-outside > section .section-main {
            min-width: 0px !important;
            max-width: 800px !important;
        }

        .index-outside > section {
            padding-bottom: 0px !important;
        }

        .layui-form-checkbox[lay-skin=primary] {
            height: auto !important;
            line-height: normal !important;
            border: none !important;
            margin-right: 0;
            padding-right: 0;
            background: 0 0
        }

        .layui-form-checkbox i {
            position: absolute;
            right: 0;
            width: 30px;
            color: #fff;
            font-size: 20px;
            text-align: center;
        }

        .layui-icon {
            font-family: layui-icon !important;
            font-size: 16px;
            font-style: normal;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale
        }

        .layui-form-checkbox[lay-skin=primary] span {
            float: right;
            padding-right: 15px;
            line-height: 18px;
            background: 0 0;
            color: #666
        }

        .layui-form-checkbox[lay-skin=primary] i {
            position: relative;
            top: 0;
            width: 16px;
            line-height: 16px;
            border: 1px solid #d2d2d2;
            font-size: 12px;
            border-radius: 2px;
            background-color: #fff;
            -webkit-transition: .1s linear;
            transition: .1s linear
        }

        .layui-form-checkbox[lay-skin=primary]:hover i {
            border-color: #e8473f;
            color: #fff
        }

        .layui-form-checked[lay-skin=primary] i {
            border-color: #e8473f;
            background-color: #e8473f;
            color: #fff
        }

        .layui-checkbox-disbaled[lay-skin=primary] span {
            background: 0 0 !important
        }

        .layui-checkbox-disbaled[lay-skin=primary]:hover i {
            border-color: #d2d2d2
        }

        .layui-form-item .layui-form-checkbox[lay-skin=primary] {
            margin-top: 10px
        }

        .index-outside >section .section-main {
            margin-bottom: 0px;
        }
    </style>
</head>
<body>
<!--头部-->
<div class="index-outside">
    <!--内容-->
    <section class="no-fix">
        <div class="section-main" style="padding: 15px;">
            <form class="layui-form" action="${ctx}/gift/gift/${giftOutput.type}/selectGift.do" id="myForm" method="post">
                <input type="hidden" name="pageNo" id="pageNo" value="${page.page}"/>
                <input type="hidden" name="targetId" value="${targetId}"/>
                <input type="hidden" name="giftIds" value="${giftIds}"/>
                <div class="f-search-bar">
                    <div class="search-container">
                        <ul class="search-form-content">
                            <li class="form-item-inline"><label class="search-form-lable">礼包名称</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="name" autocomplete="off" class="layui-input" value="${giftOutput.name}" placeholder="请输入查询礼包名称" style="width: 200px;">
                                </div>
                            </li>
                            <li class="form-item-inline">
                                <div class="sub-btns">
                                    <a class="layui-btn layui-btn-danger" id="submitBtn">确定</a>
                                    <a class="layui-btn layui-btn-normal" id="resetBtn">重置</a>
                                </div>
                            </li>
                        </ul>
                        <ul class="search-form-content">
                            <li class="form-item">
                                <label class="search-form-lable">发放时间</label>
                                <div class="check-btn-inner">
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),1,'#myForm')" ${giftOutput.progress == 1 ? 'class="active"' : ''}>进行中</a>
                                    <a href="javascript:void(0);"
                                       onclick="setTimeType($(this),2,'#myForm')" ${giftOutput.progress == 2 ? 'class="active"' : ''}>未开始</a>
                                    <input type="hidden" name="progress" value="${giftOutput.progress}"/>
                                </div>
                            </li>
                        </ul>
                    </div>
                </div>
            </form>
            <div class="c-time-list-content" style="padding-top: 0px;">
                <input type="hidden" id="giftIds"/>
                <input type="hidden" id="giftNames"/>
                <ul>
                    <table class="layui-table layui-form">
                        <colgroup>
                            <col width="80px">
                            <col width="120px">
                            <col>
                            <col width="100px">
                        </colgroup>
                        <thead>
                        <tr>
                            <th></th>
                            <th>发放对象</th>
                            <th>礼包名称</th>
                            <th>剩余库存</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach var="gift" items="${giftList}">
                            <tr data-id="${gift.id}" data-name="${gift.name}" data-condition="${gift.condition}">
                                <c:set var="chk" value=""/>
                                <c:forEach var="giftId" items="${giftIds}">
                                    <c:if test="${gift.id == giftId}">
                                        <c:set var="chk" value="true"/>
                                    </c:if>
                                </c:forEach>
                                <td>
                                    <input type="checkbox" ${chk == true ? 'checked="checked"' : ''} lay-skin="primary"
                                           name="id" value="${gift.id}" data-name="${gift.name}" lay-filter="giftChk" title="&nbsp;">
                                </td>
                                <td class="condition" data-condition="${gift.condition}">
                                    <c:choose>
                                        <c:when test="${gift.condition == 'ZC_SUPPORT'}">众筹支持</c:when>
                                        <c:when test="${gift.condition == 'ZC_APPLY'}">众筹报名</c:when>
                                        <c:when test="${gift.condition == 'ZC_ING'}">众筹鼓励</c:when>
                                        <c:when test="${gift.condition == 'ZC_SUCCESS'}">众筹成功</c:when>
                                    </c:choose>
                                </td>
                                <td class="ellipsis-1">${gift.name}</td>
                                <td>${gift.stock - gift.salesNum}</td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </ul>
            </div>
            <div id="page_content" class="page-container"></div>
        </div>
    </section>
</div>
<!--底部-->
<%@include file="../include/incScript.jsp" %>
<script type="text/javascript" src="${ctx}/script/common/list.js"></script>
<script type="text/javascript">
    var storage = localStorage;
    $(function () {
        storage.setItem("myGift_${targetId}", '${giftsJson}');
        // 加载分页
        loadPageNew({
            elem: 'page_content',
            totalCount: '${page.totalCount}',
            limit: '${page.limit}',
            page: '${page.page}'
        });
        layui.use(['form', 'laypage'], function () {
            var form = layui.form;

            form.on('checkbox(giftChk)', function (data) {
                var giftJson = storage.getItem("myGift_${targetId}");
                var _giftArray = eval(giftJson);

                var index = -1; // 如果不存在返回-1，否则返回index
                $.each(_giftArray, function (ii, dd) {
                    if (data.value == dd.id) {
                        index = ii;
                    }
                });

                var chk = $(data.elem)[0].checked; // 是否选中
                if (chk) {
                    if (index == -1) {
                        $("tbody tr").each(function (jj, dd) {
                            var id = $(dd).data("id");
                            if (data.value == id) {
                                var obj = {
                                    id: id,
                                    name: $(dd).data("name"),
                                    condition: $(dd).data("condition")
                                };
                                _giftArray.push(obj);
                            }
                        });
                    }
                } else {
                    _giftArray.splice(index, 1);
                }

                var _idsArray = _giftArray.filter(function (arr) {
                    return true;
                }).map(function (arr) {
                    return arr.id;
                });

                storage.setItem("myGift_${targetId}", JSON.stringify(_giftArray));
                $("[name=giftIds]").val(_idsArray.join(","));
            })
        });
        $("#submitBtn").click(function () {
            $("#myForm").submit();
        });

        $("#resetBtn").click(function () {
            $("#myForm input[type=text]").val("");
        });
    });

    function doSubmit() {
        var giftJson = storage.getItem("myGift_${targetId}");
        var _giftArray = eval(giftJson);
        if (_giftArray.length == 0) {
            util.layerMsgError("请先选择礼包")
            return false;
        }
        var flag = true;
        var _idArray = new Array();
        var _nameArray = new Array();
        /*var _conditionArray = new Array();*/

        $.each(_giftArray, function (ii, dd) {
            _idArray.push(dd.id);
            _nameArray.push(dd.name);

            /*var _condition = dd.condition;
            for (var m = 0; m < _conditionArray.length; m++) {
                if (_conditionArray[m] == _condition) {
                    layer.msg('不能选同种发放对象的礼包', {
                        icon: 5,
                        time: 2000
                    });
                    flag = false;
                    return flag;
                }
            }
            _conditionArray.push(_condition);*/
        });

        var ids = _idArray.join(",");
        var names = _nameArray.join(",");
        $("#giftIds").val(ids);
        $("#giftNames").val(names);
        return flag;
    }
</script>
</body>
</html>